package com.ossean;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.databaseSource.DBSource;
import com.ossean.databaseSource.GatherDao;
import com.ossean.databaseSource.UpdateControlProjectsDao;
import com.ossean.model.GatherProjectsModel;
import com.ossean.util.MergeProjectNew;
import com.ossean.util.MergeProjectsUtil;

@Component
public class MergeProjectsOld {
	Logger logger = Logger.getLogger(this.getClass());
	@Resource
	private DBSource dbSource;
	@Resource
	private GatherDao gatherDao;
	@Resource
	private UpdateControlProjectsDao updateControlDao;
	
	@Qualifier("mergeProjectNew")
	@Autowired
	private MergeProjectNew mergeProjectNew;
	//private MergeProjectsUtil mergeProjectsUtil;
	
	private int startId1;
	
	private static String pointerTableName = "edd_pointers_test";
	private static String sourceTableName = "gather_projects_test";
	private static String targetTableName = "edd_relations_test1";
	
	private static int batchSize = 500;
	
	//读指针
    public int readPointer(String table, String source, String target, int minId){
		int pointer = minId;
		try {
			pointer = dbSource.getPointer(table, source, target);
		} catch(Exception e) {
			logger.info("No such pointer! Create one");
			dbSource.insertPointer(table, source, target, pointer);
		}
		return pointer;
	}

	public void start(String edd_relations_pointer){
		int minId = 1;
		int maxId = Integer.MAX_VALUE;
		if(edd_relations_pointer.equals("edd_relations")){
			minId = 1;
			maxId = 200000;
		}else if(edd_relations_pointer.equals("edd_relations_pointer2")){
			minId = 200001;
			maxId = 400000;
		}else if(edd_relations_pointer.equals("edd_relations_pointer3")){
			minId = 400001;
			maxId = 600000;
		}else if(edd_relations_pointer.equals("edd_relations_pointer4")){
			minId = 600001;
			maxId = 800000;
		}else if(edd_relations_pointer.equals("edd_relations_pointer_others")){
			minId = 800001;
		}else{}
		while(true){
			startId1 = readPointer(pointerTableName, sourceTableName, edd_relations_pointer, minId);
			List<GatherProjectsModel> gpmList1 = gatherDao.selectGPMList(sourceTableName, batchSize);
			if(gpmList1.size() == 0){
				try {
					logger.info("no increase item to be handled! handle update items");
					//查看有没有更新的记录需要处理 通过gather_projects表中的update_mark字段进行判断 为1表示更新了数据
//					List<UpdateControlProjects> updateProjectList = updateControlDao.findAllItems(updateControlProjectsTableName);
					//不考虑更新了 湛云没有使用update_control_projects表控制更新
//					if(updateProjectList.size() != 0){
//						//表示有需要更新的项目
//						for(UpdateControlProjects updateProject:updateProjectList){
//							//重新处理更新项目
//							GatherProjectsModel model = gatherDao.selectGPMById(sourceTableName, updateProject.getId());
//							mergeProjectsUtil.handleNewProject(model, true);
//							updateControlDao.deleteOneItem(updateControlProjectsTableName, updateProject.getId());
//						}
//					}
					List<GatherProjectsModel> updateProjectList = gatherDao.selectUpdateProjects(sourceTableName,batchSize);
					for(GatherProjectsModel model:updateProjectList){
						mergeProjectNew.handleNewProject(model, true);
						gatherDao.updateUpdateMark(sourceTableName, 1, model.getId());
					}
					logger.info("all update items have been solved. Sleep 1h");
					Thread.sleep(3600*1000);
					continue;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			for(GatherProjectsModel model1:gpmList1){
				logger.info("handling project : " + model1.getId());
//				List<GatherProjectsModel> sameProjects = gatherDao.selectGPMByUrlMD5(sourceTableName, model1.getHomepageMD5());
				
				mergeProjectNew.handleNewProject(model1, false);
				
				//如果是对更新表中的数据进行操作 则不更新指针
				dbSource.updatePointer(pointerTableName, sourceTableName, edd_relations_pointer, model1.getId() + 1);
				gatherDao.updateUpdateMark(sourceTableName, 1, model1.getId()); //需要更新update_mark字段 因为按顺序取的时候可能取到1的
			}
		}
	}
	
	
	public static void main(String[] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		MergeProjectsOld Main = applicationContext.getBean(MergeProjectsOld.class);
		Main.start(args[0]);
	}
}
